//
//
//  Created by  on 16/2/23.
//  Copyright © 2016年 . All rights reserved.
//

#import "YMHomeViewController.h"
#import "AFHTTPRequestOperation.h"
#import "AFHTTPRequestOperationManager.h"
#import "IndexThemeCell.h"
@interface YMHomeViewController ()

@end

@implementation YMHomeViewController{
   AFHTTPRequestOperationManager *_manager;
    __weak IBOutlet NSLayoutConstraint *xian_width;
    __weak IBOutlet NSLayoutConstraint *xian_height;
   SGFocusImageFrame *_bannerView;
    __weak IBOutlet NSLayoutConstraint *hViewHeight;
    __weak IBOutlet NSLayoutConstraint *moveSize;
}

- (void)viewDidLoad {
    [super viewDidLoad];
     self.view.backgroundColor = BackGray;
    _top4ViewHeight.constant=(kScreenWidth*5/11) + 36;
    if (kScreenWidth==375){
         xian_height.constant=41;
         xian_width.constant=330;
    }else if (kScreenWidth>375){
        xian_height.constant=45;
        xian_width.constant=370;

    }
    
    _manager = [AFHTTPRequestOperationManager manager];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden =YES;

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = kCFNumberFormatterNoStyle;
    self.day.method = UILabelCountingMethodEaseInOut;
    self.day.formatBlock = ^NSString* (CGFloat value)
    {
        NSString* formatted = [formatter stringFromNumber:@((int)value)];
        return [NSString stringWithFormat:@"%@",formatted];
    };
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //轮播图
    [self initfocus];
    //话题内容
    [self initword];
    [self initThemeData];
    [self initTopData];
}
- (void)initThemeData {
    [self.tableView registerClass:[IndexThemeCell class] forCellReuseIdentifier:@"IndexThemeCell"];
    [self.tableView setSeparatorInset:(UIEdgeInsetsMake(0,0, 0, 0))];
    self.tableView.rowHeight=92;
    [self.tableView initData];
    hViewHeight.constant = 590+(kScreenWidth*5/11)+(self.tableView.nilArray.count*92);
    
}
- (void)initTopData {
    if (kScreenWidth==320){
        float s =120-(100*240/300);
       moveSize.constant =s;
    }else if (kScreenWidth>320&&kScreenWidth<=375){
        float s =125-(100*250/300);
        moveSize.constant =s;
    }else if (kScreenWidth>375){
        float s =140-(100*280/300);
        moveSize.constant =s;
    }
    
    
    [self.day countFrom:0 to:100  withDuration:1.5];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; 
}
- (void)initword {
    _wordlable.text=@"aaaa啊啊啊啊aaaa啊啊啊啊aaaa啊啊啊啊aaaa啊啊啊啊aaaa啊啊啊啊aaaa啊啊啊啊aaaa啊啊啊啊aaaa啊啊啊啊aaaa啊啊啊啊aaaa啊啊啊啊aaaa啊啊啊啊aaaa啊啊啊啊aaaa啊啊啊啊aaaa啊啊啊啊aaaa啊啊啊啊aaaa啊啊啊啊aaaa啊啊啊啊aaaa啊啊啊啊aaaa啊啊啊啊aaaa啊啊啊啊aaaa啊啊啊啊aaaa啊啊啊啊aaaa啊啊啊啊aaaa啊啊啊啊";
//    CGSize size = [_wordlable.text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(290, 9999) lineBreakMode:NSLineBreakByWordWrapping];
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.alignment = NSLineBreakByWordWrapping;
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:14], NSParagraphStyleAttributeName: paragraph};
    CGSize size = [_wordlable.text boundingRectWithSize:CGSizeMake(290, 9999) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    
    if(size.height>75){
        _wordHeight.constant = 75;
        
    }else{
        _wordHeight.constant = size.height;
        
    }
}
- (void)initfocus {
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"url" ofType:@"plist"];
    NSMutableDictionary *data = [[NSMutableDictionary alloc]initWithContentsOfFile:path];
    NSString *baseurl = [data objectForKey:@"URL_BASE"];
    
    NSString *url=[NSString stringWithFormat:@"%@Notices/GetCarouselFigure",baseurl];
    NSDictionary *parameters = @{@"type":@4};
    
    AFHTTPRequestOperation*op= [_manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSError*error;
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
        NSArray *datas = [dic valueForKey:@"Datas"];
        
        if (error==nil) {
            //添加最后一张图 用于循环
            int length = (int)datas.count;
            NSMutableArray *tempArray = [NSMutableArray array];
            
            for (int i = 0 ; i < length; i++)
            {
                
                NSDictionary *dict = [datas objectAtIndex:i];
                
                [tempArray addObject:dict];
            }
            
            NSMutableArray *itemArray = [NSMutableArray arrayWithCapacity:length+2];
            if (length > 1)
            {
                NSDictionary *dict = [tempArray objectAtIndex:length-1];
                SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithDict:dict tag:-1];
                [itemArray addObject:item];
            }
            for (int i = 0; i < length; i++)
            {
                NSDictionary *dict = [tempArray objectAtIndex:i];
                SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithDict:dict tag:i];
                [itemArray addObject:item];
                
            }
            //添加第一张图 用于循环
            if (length >1)
            {
                NSDictionary *dict = [tempArray objectAtIndex:0];
                SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithDict:dict tag:length];
                [itemArray addObject:item];
            }
            _bannerView = [[SGFocusImageFrame alloc] initWithFrame:CGRectMake(0, 36, kScreenWidth, kScreenWidth*5/11) delegate:self imageItems:itemArray isAuto:YES];
            [_bannerView scrollToIndex:0];
            _bannerView.alpha = 0;
            [self.top4View addSubview:_bannerView];
            [UIView animateWithDuration:0.5 animations:^{
                _bannerView.alpha = 1;
            }];
            
            
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    op.responseSerializer = [AFCompoundResponseSerializer serializer];
    [op start];
    

}
//- (void)startCalculateCircle {
//    [self.autoCalculateCircleView drawCircleWithPercent:33
//                                               duration:2
//                                              lineWidth:13
//                                              clockwise:YES
//                                                lineCap:kCALineCapRound
//                                              fillColor:[UIColor clearColor]
//                                            strokeColor:FenColor
//                                         animatedColors:nil];
//    self.autoCalculateCircleView.percentLabel.font = [UIFont systemFontOfSize:25 weight:0.4];
//    [self.autoCalculateCircleView.percentLabel setTextColor:[UIColor whiteColor]];
//    [self.autoCalculateCircleView startAnimation];
//    
//}



- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
        //偏移量＋视图高度 ＝ 内容高度
        CGFloat h1 = scrollView.contentOffset.y + CGRectGetHeight(scrollView.bounds);
        CGFloat h2 = scrollView.contentSize.height;
        NSLog(@"jsetY:%f",h1); NSLog(@"jsetY2:%f",h2);
        if (h1 - h2 > -5 && h2 - scrollView.height > 30) {
            [self.tableView loadMoreAction];
            hViewHeight.constant = 590+(kScreenWidth*5/11)+(self.tableView.nilArray.count*92);
        }
  
}


// Auto calculate radius
@end
